var app = getApp();
var util = require('../../../utils/util.js');

Page({
  data: {
    detId: "",
    serveDet: "",
    serveText: "",
    serveTel: "",
    bannerList: [],
    categoryName: "",
    imgUrl: app.globalData.imgUrl,
    systemInfo: {},
    padBtm: "25",
    scrollFlag: true,
    prevName: "",
    prevCategoryId: "",
    sharetitle: "我是海花岛业主，我为漫屿筱筑代言！",
    shareid: 0
  },
  onLoad: function(e) {
    var that = this;
    var currentId = e.id;
    if (e.topid) {
      var topid = e.topid;
      that.setData({
        shareid: topid
      })
      console.log("topidd", topid)
    }

    console.log(e);
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          systemInfo: res,
          categoryName: e.category,
          navH: app.globalData.navHeight

        })
        if (res.model == "iPhone X") {
          that.setData({
            marH: app.globalData.navHeight + 25,
            padBtm: "50",
          })
        }
      }
    });
    that.setData({
      detId: currentId,
      prevName: e.category,
      // shareid: topid
    });
    that.getServeDet();
  },

  //立即预约
  ordernow() {
    var that = this;
    //判断token
    if (wx.getStorageSync('token') == '') {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    } else {
      that.checkToken()
      wx.navigateTo({
        url: '/pages/order/info/order-info?name=serve&id=' + that.data.detId + '&category= ' + that.data.categoryName + '&topid=' + that.data.shareid
      })
    }
  },
  checkToken: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/token/check",
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          console.log(res);
          //监测token是否过期，如果没过期就用缓存取出来的，如果过期就用刷新后的token
          if (res.data.code != 1) {
            that.refreshToken();
          } else {}
        }
      }
    });
  },
  refreshToken: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/token/refresh",
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          //刷新后修改全局的token
          wx.setStorageSync('token', res.data.data.token);
        }
      }
    });
  },
  onPageScroll: function(e) {
    var that = this;
    if (e.scrollTop >= 140) {
      that.setData({
        scrollFlag: false
      });
    } else {
      that.setData({
        scrollFlag: true
      });
    }
  },
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.sharetitle,
      // '我是海花岛业主，我为漫屿筱筑代言！'
      //中国海花岛漫屿筱筑精品民宿欢迎您
      //path: '/pages/hotel/detail/hotel-det?id=' + that.data.detId
      //path: 'pages/index/index?url=/pages/serve/detail/serve-det?id=' + that.data.detId
      path: 'pages/index/index?url=/pages/serve/detail/serve-det&id=' + that.data.detId
    }
  },
  getServeDet: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/index/goodsDetails",
      data: {
        id: that.data.detId
      },
      header: {
        'content-type': 'application/json'
      },
      method: "GET",
      dataType: "json",
      success: function(res) {
        console.log(res);
        if (res.statusCode === 200) {
          var serveData = res.data.data;
          var prevCategoryId = serveData.category_id;
          var serveBanner = serveData.pics.split(',');
          var serveBody = serveData.body;
          var serveTextCode = util.baseDecode(serveBody); //base64解码
          that.setData({
            serveDet: serveData,
            prevCategoryId: prevCategoryId,
            bannerList: serveBanner,
            serveText: serveTextCode,
            serveTel: serveData.tel,
            // shareid: res.data.data.category_id
          })
          // console.log("id", that.data.shareid)
          if (that.data.shareid == 50) {
            that.setData({
              sharetitle: "中国海花岛漫屿筱筑精品民宿欢迎您！"
            })
          }
        }
      },

    });
  },
  navBack: function() {
    // wx.redirectTo({
    //     url: '../list/serve-list?id='+this.data.prevCategoryId+'&name='+this.data.prevName
    // })
    wx.navigateBack({
      delta: 1
    })
  },
  callPhone: function() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.serveTel
    })
  }
})